package com.yumeng.common.runtime;

import com.yumeng.common.exception.YumengException;

import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;

/**
 * 全局变量
 *
 * @author wxd
 * @date 2025/11/10 14:19
 */
public class GlobalVariableHelper {

    private GlobalVariableHelper(){}
    private static GlobalVariableHelper instance;
    public synchronized static GlobalVariableHelper getInstance(){
        if (instance == null){
            synchronized (GlobalVariableHelper.class){
                if (instance == null){
                    instance = new GlobalVariableHelper();
                }
            }
        }
        return instance;
    }

    /**
     * 注册时区id，防止出现设置时区后，时区不一致的情况（正常情况下不会出现）
     *
     */
    private Set<String> timeZoneIds = new HashSet<>();
    public void registerTimeZone(String timeZoneId){
        this.timeZoneIds.add(timeZoneId);
    }
    public String getRegisterTimeZoneId(){
        String defaultId = TimeZone.getDefault().getID();
        timeZoneIds.add(defaultId);
        if (timeZoneIds.size() > 1){
            throw new YumengException("时区信息异常：%s", timeZoneIds.toString());
        }
        return timeZoneIds.iterator().next();
    }

}
